package zhixu.shell;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

public class Shell {
	
	private static String line;
	private static String [] words;
	private static String pwd;
	private static String home;
	
	public static void ls(String[] args) {
		if (args.length == 1) {
			String[] a = {"ls", pwd};
			ls(a);
			return;
		}
		
		String nfile_info = "";
		String[] dir_info = new String [args.length-1];
		int dir_index = 0;
		String[] warns = new String [args.length-1];
		int warn_index = 0;
		for (int i = 1; i < args.length; i++) {
			File file = new File(args[i]);
			if (!file.exists()) {
				warns[warn_index++] ="ls: " + args[i] + ": No such file or directory.\n";
				continue;
			}
			if (!file.isDirectory()) {
				nfile_info += args[i] + "\t"; 
				continue;
			}
			if (args.length > 2)
				dir_info[dir_index] = args[i] + ":\n";
			else
				dir_info[dir_index] = "";
			String [] names = file.list();
			for (String name:names)
				dir_info[dir_index] += name + "\t";
			dir_info[dir_index++] += "\n";			
		}
		
		for (int i = 0; i < warn_index; i++)
			System.out.printf(warns[i]);
		if (nfile_info.length() != 0)
			nfile_info += "\n";
		System.out.printf(nfile_info);
		for (int i = 0; i < dir_index; i++)
			System.out.printf(dir_info[i]);
	}
	
	public static void rm(String[] args) {
		if (args.length == 1) {
			System.out.printf("rm: not enough arguments.\n");
			return;
		}
		
		int i;
		String[] opts = new String[args.length-1];
		int opts_index = 0;
		String[] names = new String[args.length-1];
		int names_index = 0;
		for (i = 1; i < args.length; i++) {
			if (args[i].startsWith("-") || args[i].startsWith("--"))
				opts[opts_index++] = args[i];
			else
				names[names_index++] = args[i];
		}
		
		boolean isRecursive = false;
		for (i = 0; i < opts_index++; i++) {
			if ("-r".equals(opts[opts_index]) || "-R".equals(opts[opts_index]) || "--recursive".equals(opts[opts_index])) {
				isRecursive = true;
			} else {
				System.out.printf("rm: " + opts[opts_index] +": Unknown options.\n");
				return;
			}			
		}
		
		if (names.length == 0) {
			System.out.printf("rm: not enough arguments.\n");
			return;
		}
		
		String[] warns = new String[args.length-1];
		int warn_index = 0;
		for (i = 0; i < names_index; i++) {
			File file = new File(names[i]);
			if (!file.exists()) {
				warns[warn_index++] = "rm: " + names[i] + ": No such file or directory.\n";
				continue;
			}
			if (file.isDirectory()) {
				if (isRecursive) {
					String[] a = {"rm", "-r", names[names_index]+"/*"};
					rm(a);
					file.delete();
				}
				continue;
			}
			file.delete();
		}
		for (i = 0; i < warn_index; i++)
			System.out.printf(warns[i]);
	}
	
	public static void main(String[] args) throws Exception{
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		System.out.printf("Login as: ");
		String username = reader.readLine();
		System.out.printf("Password: ");
		String password = reader.readLine();
		
		if (!"123".equals(password))
			return;
		
		pwd = System.getProperty("user.dir");
		home = pwd;
		System.out.println(pwd);
		while (true) {
			System.out.printf(">> ");
			line = reader.readLine();
			words = line.split(" ");
			
			if ("cd".equals(words[0])) {
				if (words.length == 1) {
					pwd = home;
					continue;
				}
				File dir = new File(words[1]);
				if (dir.isDirectory())
					pwd = words[1]; //TODO: pwd shouldn't be like this
				else
					System.out.printf("cd: " + words[1] + ": Not a directory.\n");
			}
			if ("ls".equals(words[0])) {
				ls(words);
			}
			if ("rm".equals(words[0])) {
				rm(words);
			}
			if ("exit".equals(words[0])) {
				break;
			}	
		}
	}
}